/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System;
using System.Collections;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// Column chart
	/// </summary>
	/// <example>
	/// <br>EQColumnChart chart = new EQColumnChart(EQColumnChartType.Column3D);</br>
	/// <br>//It will increase a column analysis chart by each time calling AddDataColumnIndex()</br>
	/// <br>chart.AddDataColumnIndex(1, "Price");	</br>
	/// <br>chart.AddDataColumnIndex(2, "Sale");</br>
	/// <br>chart.AddDataColumnIndex(3, "Gain");</br>
	/// <br>chart.LabelColumnIndex = 0;	//Bind label column index</br>
	/// <br>chart.Top = 3;</br>
	/// <br>chart.Left = 6;</br>
	/// <br>objEQTable.Chart = chart;</br>
	/// </example>
	public class EQColumnChart: EQSeriesChart
	{
		/// <summary>
		/// Set/Get the column chart type
		/// </summary>
		public new EQColumnChartType ChartType
		{
			get
			{
				return EQEnumKit.ToEQColumnChartType(base.ChartType);
			}
			set
			{
				base.ChartType = EQEnumKit.ToXlChartType(value);
			}
		}

		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="chartType">Column chart type</param>
		public EQColumnChart(EQColumnChartType chartType)
		{
			this.ChartType = chartType;
		}
		

		/// <summary>
		/// Output this object to excel document
		/// </summary>
		/// <param name="objWorksheet"></param>
		protected internal override void Output(Excel.Worksheet objWorksheet)
		{
			this.DoBeforeOperationEvent(objWorksheet);

			//*****Set the chart source and get Excel.Chart COM interface*****
			Excel.Chart objChart = this.InitializeChart(objWorksheet);

			int seriesCount = ((Excel.SeriesCollection)objChart.SeriesCollection(this.Missing)).Count;
			while(seriesCount>0)	//Delete all series
			{
				Excel.Series objSeries = (Excel.Series)objChart.SeriesCollection(1);
				objSeries.Delete();
				seriesCount --;
			}

			Excel.SeriesCollection objSeriesCollection = objChart.SeriesCollection(this.Missing) as Excel.SeriesCollection;
			for(int i=0; i<this.DataColumnIndexs.Count; i++)	//Generate N series
				objSeriesCollection.NewSeries();

			//Selection of XValues
			int intLabelColumnIndex = this.ChartSource.TableLeft + this.LabelColumnIndex;
			int intTableBottom = this.ChartSource.ItemTop + this.ChartSource.Table.Rows.Count - 1;	//Get the last row index of table
			Excel.Range xValues = objWorksheet.get_Range(objWorksheet.Cells[this.ChartSource.ItemTop, intLabelColumnIndex], objWorksheet.Cells[intTableBottom, intLabelColumnIndex]) as Excel.Range;

			int index = 0;
			IDictionaryEnumerator objIDictionaryEnumerator = this.DataColumnIndexs.GetEnumerator();
			while(objIDictionaryEnumerator.MoveNext())	//Set every series one by one
			{
				Excel.Series objSeries = (Excel.Series)objChart.SeriesCollection(index+1);
				objSeries.Name = objIDictionaryEnumerator.Value as string;

				int intDataColumnIndex = this.ChartSource.TableLeft + (int)objIDictionaryEnumerator.Key;	//Mapping datatable of the object to column index on excel document
				Excel.Range objRange = objWorksheet.get_Range(objWorksheet.Cells[this.ChartSource.ItemTop, intDataColumnIndex], objWorksheet.Cells[intTableBottom, intDataColumnIndex]) as Excel.Range;
				objSeries.Values = objRange;
				objSeries.XValues = xValues;
				index ++;
			}

			base.Output(objWorksheet);

			this.DoAfterOperationEvent(objWorksheet);
		}

	}
}
